﻿@using Models.Dto
@using System.Security.Claims
<CascadingAuthenticationState >
    <BootstrapBlazorRoot>
        <CascadingValue Value="this.GlobalInfo">
            <CascadingValue Value="this.FunctionGroups" Name="router">
                <Router AppAssembly="@typeof(BlazorAuto.Client.Routes).Assembly" AdditionalAssemblies="new System.Reflection.Assembly[]{typeof(Shared.Page.Runing).Assembly}">
                    <Found Context="routeData">
                        <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Shared.Layout.MainLayout)">
                            <NotAuthorized>
                                没有经过权限认证的地址
                            </NotAuthorized>
                        </AuthorizeRouteView>
                        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
                    </Found>
                    <NotFound>
                        <LayoutView Layout="@typeof(Shared.Layout.MainLayout)">
                            <p role="alert">对不起,这个访问地址不存在</p>
                        </LayoutView>
                    </NotFound>
                </Router>
            </CascadingValue>
        </CascadingValue>
    </BootstrapBlazorRoot>
</CascadingAuthenticationState>
@code
{
    public GlobalInfo GlobalInfo { get; set; } = new GlobalInfo() { User = new User() { RealName = "未登录" } };

    public List<FunctionGroup>? FunctionGroups { get; set; }

    [Inject, NotNull]
    private AuthenticationStateProvider? AuthenticationStateProvider { get; set; }

    protected override async Task OnInitializedAsync()
    {
        System.Console.WriteLine(DateTime.Now+":Router刷新");
        
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState?.User;
        
        if (user?.Identity is not null && user.Identity.IsAuthenticated)
        {
            var modname = user.Claims.FirstOrDefault(x => x.Type.ToLower() == "modulename")?.Value ?? "";
            GlobalInfo.Url = user.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Uri)?.Value ?? "服务器地址为空";
        }
        await base.OnInitializedAsync();
    }
}